package com.ly.lw.styledo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.CategoryGridAdapter;
import com.ly.lw.styledo.bean.RecBean;
import com.ly.lw.styledo.config.UrlCategory;
import com.ly.lw.styledo.ui.CategoryInfoActivity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 属于分类的碎片:默认显示“推荐的数据”
 * 王学玲
 */
public class CategoryFragment extends Fragment implements AdapterView.OnItemClickListener{
    //控件、数据源、适配器
    @ViewInject(R.id.category_tuijian)
    private GridView tjGridView;
    private ArrayList<RecBean> datas;
    private CategoryGridAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        ViewUtils.inject(this, view);
        initView();
        initData();
        return view;
    }

    /**
     * 初始化数据
     */
    private void initView() {
        //推荐
        datas = new ArrayList<>();
        adapter = new CategoryGridAdapter(datas, getActivity());
        tjGridView.setAdapter(adapter);
        tjGridView.setOnItemClickListener(this);
    }

    /**
     * 推荐：初始化数据
     */
    private void initData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = getRequestParms();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlCategory.CATEGORY_URL, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                try {
                    JSONArray res = new JSONObject(result).optJSONObject("result").optJSONArray("rec_list");
                    if (res != null) {
                        List<RecBean> recLists = JSON.parseArray(res.toString(), RecBean.class);
                        if (recLists != null && recLists.size() > 0) {
                            datas.addAll(recLists);
                            adapter.notifyDataSetChanged();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
//                Log.i("info", "加载数据失败");
            }
        });
    }


    /**
     * 分类一级页面请求参数
     *
     * @return
     */
    private RequestParams getRequestParms() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("up_cate_no", "0");
        params.addBodyParameter("appVersion", "1.4.6");
        params.addBodyParameter("os", "android");
        params.addBodyParameter("osVersion", "4.1.2");
        return params;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Toast.makeText(getActivity(), "---->" + datas.get(position).getCateno(), Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(getActivity(), CategoryInfoActivity.class);
        intent.putExtra("cateno",datas.get(position).getCateno());
        intent.putExtra("catename",datas.get(position).getCate_nm());
        startActivity(intent);
    }
}
